package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.*;
import com.sc.nft.entity.*;
import com.sc.nft.entity.dto.AddUserCityServerCentreExtraAwardDTO;
import com.sc.nft.entity.dto.SaveUserCityServerCentreExtraAwardDTO;
import com.sc.nft.entity.dto.UrbanServiceCenterPointsDTO;
import com.sc.nft.entity.dto.UserCityServerCentreExtraAwardPageListDTO;
import com.sc.nft.entity.vo.UserCityServerCentreExtraAwardPageListVO;
import com.sc.nft.enums.ExpenditureRecordTypeEnum;
import com.sc.nft.enums.IncomeHierarchyEnum;
import com.sc.nft.enums.IncomeTypeEnum;
import com.sc.nft.enums.WalletDetailTypeEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.pool.SysMemberPool;
import com.sc.nft.service.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@Service
public class UserCityServerCentreExtraAwardServiceImpl implements UserCityServerCentreExtraAwardService {

    private final UserCityServerCentreExtraAwardDao userCityServerCentreExtraAwardDao;

    private final UserCityServerCentreExtraAwardLogService userCityServerCentreExtraAwardLogService;

    private final UserCityServerCentreDao userCityServerCentreDao;

    private final UserInfoDao userInfoDao;
    private final UserWalletDao userWalletDao;

    private final SecondOrderDao secondOrderDao;

    private final UserWalletService userWalletService;

    private final UserIncomeRecordDao userIncomeRecordDao;
    private final ShareTheIncomeRecordService shareTheIncomeRecordService;
    private final ExpenditureRecordService expenditureRecordService;

    @Override
    public UserCityServerCentreExtraAward getByUserIdAndSumGtZero(Long userId) {
        UserCityServerCentreExtraAward byUserIdAndSumGtZero = userCityServerCentreExtraAwardDao.getByUserIdAndSumGtZero(userId);
        return byUserIdAndSumGtZero;
    }

    @Override
    public Page<UserCityServerCentreExtraAwardPageListVO> getPageList(UserCityServerCentreExtraAwardPageListDTO userCityServerCentreExtraAwardPageListDTO) {
        return userCityServerCentreExtraAwardDao.getPageList(userCityServerCentreExtraAwardPageListDTO);
    }

    @Override
    public void saveSumSubtractOne(Long id) {
        userCityServerCentreExtraAwardDao.saveSumSubtractOne(id);
    }

    @Override
    public void deleteById(Long id) {
        userCityServerCentreExtraAwardDao.deleteById(id);
    }

    @Override
    public void add(AddUserCityServerCentreExtraAwardDTO addUserCityServerCentreExtraAwardDTO) {
        UserCityServerCentreExtraAward award = new UserCityServerCentreExtraAward();
        BeanUtil.copyProperties(addUserCityServerCentreExtraAwardDTO, award);
        award.setCreateId(SysMemberPool.getMember().getId());
        award.insert();
        UserInfo byId = userInfoDao.getById(addUserCityServerCentreExtraAwardDTO.getUserId());
        if (BeanUtil.isEmpty(byId)) {
            return;
        }
        UserCityServerCentre userCityServerCentre = userCityServerCentreDao.selectByUserId(addUserCityServerCentreExtraAwardDTO.getUserId());
        if (BeanUtil.isEmpty(userCityServerCentre)) {
            return;
        }
        // 写入日志
        String format = StrUtil.format("{}新增了城市服务中心[{}} | {}]", SysMemberPool.getMember().getUserName(), byId.getUserTel(), StrUtil.format("{}-{}-{}", userCityServerCentre.getProvince(), userCityServerCentre.getCity(), userCityServerCentre.getDistrict()));
        UserCityServerCentreExtraAwardLog userCityServerCentreExtraAwardLog = new UserCityServerCentreExtraAwardLog();
        userCityServerCentreExtraAwardLog.setRemark(format);
        userCityServerCentreExtraAwardLogService.add(userCityServerCentreExtraAwardLog);
    }

    @Override
    public void updateById(List<SaveUserCityServerCentreExtraAwardDTO> saveUserCityServerCentreExtraAwardDTO) {
        saveUserCityServerCentreExtraAwardDTO.forEach(data -> {
            UserCityServerCentreExtraAward userCityServerCentreExtraAward = userCityServerCentreExtraAwardDao.getById(data.getId());
            UserInfo userInfo = userInfoDao.getById(userCityServerCentreExtraAward.getUserId());
            String remark = SysMemberPool.getMember().getUserName() + "修改了手机号用户:" + userInfo.getUserTel() + "的";
            if (data.getSum() != null) {
                remark = remark + StrUtil.format("结算次数为{}次,", data.getSum());
            }
            if (data.getFirstPublish() != null) {
                remark = remark + StrUtil.format("市场补贴(额外首发分红)的比例为[{}%],", data.getFirstPublish());
            }
            if (data.getConsignmentSales() != null) {
                remark = remark + StrUtil.format("市场补贴(额外寄售分红)的比例[{}%],", data.getConsignmentSales());
            }
            if (data.getRemark() != null) {
                remark = remark + StrUtil.format("修改了备注[{}],", data.getRemark());
            }
            UserCityServerCentreExtraAward award = new UserCityServerCentreExtraAward();
            BeanUtil.copyProperties(data, award);
            award.updateById();
            UserCityServerCentreExtraAwardLog userCityServerCentreExtraAwardLog = new UserCityServerCentreExtraAwardLog();
            userCityServerCentreExtraAwardLog.setRemark(remark.substring(0, remark.length() - 1));
            userCityServerCentreExtraAwardLogService.add(userCityServerCentreExtraAwardLog);
        });
    }




}
